package ua.sumdu.LukyanovChalyi.Server;

import java.io.*;
import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

public class ReadWriteTextFile implements ReadWriteFiles {

    @Override
    public void write(Map<Long,CopyBook> books, String filename) throws IOException {
        Writer out = null;
        try {
            out = (new BufferedWriter(new FileWriter(filename)));
            this.write(books, out);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    /**
     * Example: "ID:324; Authors: Edgar Allan Poe, James Fenimore Cooper; On hand:yes; Title:sdfs; Date:1992; Number of pages:342;"
     */
    public void write(Map<Long,CopyBook> books, Writer out) throws IOException {
        PrintWriter outWriter = new PrintWriter(out);
        Set<Long> keys = books.keySet();
        for (Long key : keys) {
            CopyBook cb = books.get(key);
            outWriter.print("ID:" + key + "; ");
            List<String> authors = cb.getBook().getAuthors();
            outWriter.print("Authors: ");
            for (String name : authors) {
                outWriter.print(name + ", ");
            }
            outWriter.print("; ");
            outWriter.print("On hand:" + cb.isActive() + "; ");
            outWriter.print("Title:" + cb.getBook().getTitle() + "; ");
            outWriter.print("Date:" + cb.getBook().getDate() + "; ");
            outWriter.print("Number of pages:" + cb.getBook().getNumberOfPages());
            outWriter.println();
        }
    }

    @Override
    public Map<Long,CopyBook> read(String filename) throws IOException {
        Reader inStream = null;
        Map<Long,CopyBook> books = null;
        try {
            inStream = new FileReader(filename);
            books = this.read(inStream);
            return books;
        } finally {
            if (inStream != null) {
                inStream.close();
            }
            if (books == null) {
                return null;
            }
        }
    }

    /**
     * Example: "ID:324; Authors: Edgar Allan Poe, James Fenimore Cooper; On hand:yes; Title:sdfs; Date:1992; Number of pages:342;"
     */
    public Map<Long,CopyBook> read(Reader in) throws IOException {
        BufferedReader inStream = new BufferedReader(in);
        String str = null;
        Map<Long,CopyBook> books = new HashMap<Long,CopyBook>();
        while ((str = inStream.readLine()) != null) {
            String[] arr = str.split(":|;");
            List<String> authors = new ArrayList<String>();
            StringTokenizer stoken = new StringTokenizer(arr[3], ", ");
            while (stoken.hasMoreTokens()) {
                authors.add(stoken.nextToken());
            }
            boolean onHand = false;
            if (arr[5].equals("yes")) {
                onHand = true;
            }
            books.put(new Long(arr[1]), new CopyBook(new Book(authors, arr[7],
                    Integer.valueOf(arr[9]), Integer.valueOf(arr[11])),
                    new Long(arr[1]), onHand));
        }
        return books;
    }
}